package XMLManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.os.Environment;
import android.util.Log;


public class yXMLReader {
	private FileInputStream fis;
	private InputStreamReader isr;
	private char [] ib;
	private XmlPullParserFactory factory;
	private XmlPullParser xpp;
	private String data;
	private int eventType;
	private String filename;
	private File file;
	
	public static final int START_TAG = XmlPullParser.START_TAG;
	public static final int END_TAG = XmlPullParser.END_TAG;
	public static final int START_DOCUMENT = XmlPullParser.START_DOCUMENT;
	public static final int END_DOCUMENT = XmlPullParser.END_DOCUMENT;
	public static final int TEXT = XmlPullParser.TEXT;
	
	public yXMLReader(String filename){
		try{
			this.filename = filename;
			file = new File(Environment.getExternalStorageDirectory() + "/" +
			filename);
			Log.d("XML OPEN:", ""+file.exists());
			fis = new FileInputStream(file);

			ib = new char[fis.available()];
			isr = new InputStreamReader(fis);
			isr.read(ib);
			data = new String(ib);
			isr.close();
			fis.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public boolean open(){
		if(data == null) return false;
		
		try {
			factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			xpp = factory.newPullParser();
			
			StringReader sr = new StringReader(data);
			xpp.setInput(sr);
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
		return true;
	}
	public int event(){
		try {
			eventType = xpp.next();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return eventType;
	}
	public String getName(){
		return xpp.getName();
	}
	public String getText(){
		return xpp.getText();
	}
	public String getAttr(String attr){
		return xpp.getAttributeValue(null, attr);
	}
	
	public void close(){
		
	}
}